//
//  RTJHTMLViewController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/14.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJHTMLViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "RTJTenderDetailsController.h"
#import "RTJTabBarController.h"
@interface RTJHTMLViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation RTJHTMLViewController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}

- (void)viewDidLoad {
    
    [SVProgressHUD showWithStatus:nil];

    [super viewDidLoad];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    self.webView.userInteractionEnabled = YES;

    if (_isUrl) {
        
        if ([self.title isEqualToString:@"合规进展"]||[self.title isEqualToString:@"详情"]) {
            
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_htmlStr]]];
            
        }else{
            
            [self.webView loadHTMLString:_htmlStr baseURL:nil];

        }
        
    }else{
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:_htmlStr ofType:@"html"];
        NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSURL *url = [[NSURL alloc] initWithString:filePath];
        [self.webView loadHTMLString:htmlString baseURL:url];
    }
    
}

//开始加载网页
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [SVProgressHUD showWithStatus:nil];
    
}

//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [SVProgressHUD dismiss];
//    JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//
//    //此处的toPage和JS方法中的toPage名称一致.
//    content[@"jumpApp"] = ^() {
//
//        NSArray *arguments = [JSContext currentArguments];
//        for (JSValue *jsValue in arguments) {
//            NSLog(@"=======%@",jsValue);
//        }
//    };
}

//网页加载错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *requestString = [[[request URL] absoluteString]stringByRemovingPercentEncoding];
    NSLog(@"requestString : %@",requestString);

    return [self detailsViewControllerForUrl:requestString];
    
}

-(BOOL)detailsViewControllerForUrl:(NSString *)requestString {
    
    
    if ([requestString containsString:@"http://m.rtc.ruitaijin.com/lend?id="]){
        
        NSArray *array = [requestString componentsSeparatedByString:@"="]; //从字符中分隔成2个元素的数组
        RTJTenderDetailsController *VC = [[RTJTenderDetailsController alloc]init];
        VC.markId = array[1];
        [self.navigationController pushViewController:VC animated:YES];
        
        return NO;
        
    } else  if ([requestString containsString:@"http://m.rtc.ruitaijin.com/lend"]) {
        
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        RTJTabBarController *tabViewController = (RTJTabBarController *) appDelegate.window.rootViewController;
        [tabViewController setSelectedIndex:1];
        
        return NO;
        
    }else{
        
        return YES;
    }
    
    
}

@end
